package com.joyzone.weixin.service;

import com.joyzone.weixin.exception.WeixinException;
import com.joyzone.weixin.security.AccessToken;
import com.joyzone.weixin.security.OAuthToken;
import com.joyzone.weixin.util.CommonUtil;
import com.joyzone.weixin.util.Constants;
import com.joyzone.weixin.util.StringUtil;

public class TokenService {
	
	/**
	 * 获取access token
	 */
	public static AccessToken getAccessToken() throws WeixinException {
		return new AccessToken(CommonUtil.httpsRequest2Json(Constants.URL_Get_Access_Token, "GET", null)) ;
	}

	/**
	 * OAuth2.0通过code换取网页授权access_token
	 * @param code
	 * @return
	 * @throws WeixinException 
	 */
	public static OAuthToken getOAuthToken(String code) throws WeixinException  {
		String url = StringUtil.formatParaUrl(Constants.Url_Oauth2_Access_Token,new String[]{code} );
		return new OAuthToken(CommonUtil.httpsRequest2Json(url, "GET", null));
	}
	
	/**
	 * 刷新access_token
	 * @param refresh_token 填写通过access_token获取到的refresh_token参数
	 * @return
	 */
	public static OAuthToken refreshOAuthToken(String refresh_token) throws WeixinException {
		String url = StringUtil.formatParaUrl(Constants.Url_Oauth2_Refresh_Token,new String[]{refresh_token} );
		return new OAuthToken(CommonUtil.httpsRequest2Json(url, "GET", null));
	}
}
